/**
 * 
 */
package com.fox.webapp.web.admin;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fox.webapp.domain.Article;
import com.fox.webapp.service.IArticleService;

/**
 * @author yockii
 *
 */
@Controller
@RequestMapping("admin/article")
public class ArticleAdminController {
	private static final Logger logger = LoggerFactory.getLogger(ArticleAdminController.class);
	@Autowired
	private IArticleService articleService;
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addArticlePage(){
		return "admin/article/add";
	}
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateArticle(Model model, @PathVariable String id){
		model.addAttribute("article", articleService.getArticle(id));
		return addArticlePage();
	}
	
	@RequestMapping(value = "show", method = RequestMethod.GET)
	public String allArticlePage(Model model){
		List<Article> articleList = articleService.getAllArticle();
		model.addAttribute("articleList", articleList);
		return "admin/article/show";
	}
	
	@RequestMapping(value = "add", method = {RequestMethod.POST, RequestMethod.PUT})
	public String addNewArticle(Article article, Model model){
		articleService.addArticle(article);
		return allArticlePage(model);
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String updateArticle(Article article, Model model){
		articleService.updateArticle(article);
		return allArticlePage(model);
	}
	
	@RequestMapping(value = "delete", method = {RequestMethod.POST, RequestMethod.DELETE})
	@ResponseBody
	public String deleteArticle(String id){
		articleService.deleteArticle(id);
		return "1";
	}
}
